package com.bible.fire.util.http;

import java.io.File;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;

import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;

import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;


import com.bible.fire.bean.common.UploadFile;
import com.bible.fire.local.RuntimeContext;
import com.bible.fire.util.AndroidUtil;

import android.os.Handler;
import android.util.Log;


public class HttpClientHelper {
	
	private static String appURL = "http://192.168.1.111:8080/default/";
	//private static String appURL = "http://192.168.2.106:8080/default/";
	
	private static HttpPost createPost(String requestUrl ){
		HttpPost httpost =  new HttpPost( requestUrl );
		httpost.addHeader("connection","keep-alive");
		httpost.addHeader("Cache-Control","no-cache");
		Object sessionId = AndroidUtil.getAttribute("sessionID");
		if( sessionId != null ){
			httpost.setHeader("Cookie", "JSESSIONID=" + sessionId.toString() );
		}	
		return httpost;
	}
	
	public static boolean sendHttpRequest(String requestUrl, Map<String, Object> paramMap, Handler handler, int connectionTimeout, int socketTimeout ){
		boolean result = true;
		HttpPost httpost =  createPost( requestUrl );
		httpost.addHeader("X_REQUESTED_WITH","XMLHttpRequest");
		httpost.addHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
		List<NameValuePair> paramList = new ArrayList<NameValuePair>(); 
		if( paramMap != null && paramMap.size() > 0 ){
			for(String key : paramMap.keySet() ){
				paramList.add(new BasicNameValuePair( key, paramMap.get(key).toString()));
			}
		}

		Log.d("executing request ",  httpost.getURI().toString() );
		try {
			httpost.setEntity(new UrlEncodedFormEntity(paramList, HTTP.UTF_8));
			/*
			HttpThread thread = new HttpThread( handler, connectionTimeout, socketTimeout );
			thread.doStart( httpost );
			*/
			HttpTask task = new HttpTask( httpost, handler, connectionTimeout, socketTimeout );
			RuntimeContext.getHttpThreadPool().execute(task);
		} catch (Exception e) {
			result = false;
			Log.e("http thread", "httpost error", e);
		}		 
		return result;
	}
	
	public static boolean uploadFile(String requestUrl, Map<String, Object> paramMap, List<UploadFile> fileList, Handler handler){
		boolean result = true;
		HttpPost httpost =  createPost( requestUrl );			
		try {
			MultipartEntity entity = new MultipartEntity();
			List<NameValuePair> paramList = new ArrayList<NameValuePair>(); 
			if( paramMap != null && paramMap.size() > 0 ){
				for(String key : paramMap.keySet() ){
					paramList.add(new BasicNameValuePair( key, paramMap.get(key).toString()));
					entity.addPart( key, new StringBody(paramMap.get(key).toString(), Charset.forName("UTF-8")) );
				}
			}
			if( fileList != null && fileList.size() > 0 ){
				for( UploadFile uFile : fileList ){
					File file = new File(uFile.getFilepath());
					if( file.exists() ){
						entity.addPart( uFile.getParamName(), new FileBody(file) );
					}
				}
			}
			httpost.setEntity(entity);
			/*
			HttpThread thread = new HttpThread( handler );
			thread.doStart( httpost );
			*/
			HttpTask task = new HttpTask( httpost, handler );
			RuntimeContext.getHttpThreadPool().execute(task);
		} catch (Exception e) {
			result = false;
			Log.e("http upload file", "httpost error", e);
		}	
		return result;
	}
	
	public static boolean downloadFile(String[] requestUrl, String[] fileformat, Map<String, Object> paramMap, Handler handler){
		if( requestUrl == null || fileformat == null || requestUrl.length != fileformat.length ){
			return false;
		}		
		boolean result = true;
		HttpPost httpost =  createPost( "" );			
		List<NameValuePair> paramList = new ArrayList<NameValuePair>(); 
		if( paramMap != null && paramMap.size() > 0 ){
			for(String key : paramMap.keySet() ){
				paramList.add(new BasicNameValuePair( key, paramMap.get(key).toString()));
			}
		}
		//Log.d("executing request ",  httpost.getURI().toString() );
		try {
			httpost.setEntity(new UrlEncodedFormEntity(paramList, HTTP.UTF_8));
			/*
			HttpThread thread = new HttpThread( handler );
			thread.doStart( httpost, true, requestUrl, fileformat );
			*/
			HttpTask task = new HttpTask( httpost, handler );
			task.setDownloadAttr(true, requestUrl, fileformat);
			RuntimeContext.getHttpThreadPool().execute(task);
		} catch (Exception e) {
			result = false;
			Log.e("http thread", "httpost error", e);
		}	
		return result;
	}
	
	
}
